let index = 0
const cheerio = require('cheerio');

let getHotNews = (res) => {
  let hotNews = [];

  var $ = cheerio.load(res.text, {decodeEntities: false});

  $('script').each((idx, ele) => {
   if(idx === 8) {
    hotNews = JSON.parse(JSON.stringify(ele.children[0]?.data))   // 存入最终结果数组
    const re = /{[\S\s]*}/;
    const match = hotNews.match(re);
      if(match){
      eval('var obj =' + match[0])
      hotNews = obj
      }
   }            
  });
  return hotNews
};

// 解决 superagent 中文乱码问题
const charset = require('superagent-charset');
const superagent = charset(require('superagent'));

let hotNews = [];                                // 热点新闻
let localNews = [];                              // 本地新闻

/**
 * index.js
 * [description] - 使用superagent.get()方法
 */
async function fetch (number, cityId = '000000', type) {
  let result = []
  const headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko',
    'Accept-Language': 'zh-Hans-CN, zh-Hans;q=0.5'
  }
  let arr = []
  /*
  并行请求多页面数据并包装为Promise.all异步操作返回
  */
  let i = 0  
  let resArr = []
  console.log('09877213123',cityId)
  return new Promise((resolve, reject) => {
    const timer = setInterval(async () => {
      const URL = `https://search.51job.com/list/${cityId},000000,0000,00,9,99,web%25E5%2589%258D%25E7%25AB%25AF,2,${i}.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=`
          await superagent.get(URL).charset('gbk').set(headers).end(async (err, res) => {
          if (err) {
            console.log(`51job抓取失败 - ${err}`)
          } else {
           result = await getHotNews(res)
           console.log( URL)
           resArr.push(result)
          }
        })
          i++
        if(i === 5) {
          clearInterval(timer)
          resolve(resArr)
        }
      }, 100)
  })

}

module.exports = fetch